package cn.wolfcode.ssm.shiro;

import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.mapper.PermissionMapper;
import cn.wolfcode.ssm.mapper.RoleMapper;
import cn.wolfcode.ssm.service.IEmployeeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

//我们通常需要使用到缓存，认证和授权所有的功能，所以选择继承AuthorizingRealm。
@Component("crmRealm")
public class CRMRealm extends AuthorizingRealm {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    //将容器中配置的凭证匹配器注入到当前的Realm对象中
    @Autowired
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        super.setCredentialsMatcher(credentialsMatcher);
    }
        //获取认证信息
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //拿到用户页面输入的用户名  拿身份(通常是用户名)
        String username = (String)token.getPrincipal();
        Employee employee = employeeService.selectByName(username);
        //如果查到了，返回一个AuthenticationInfo对象
        if (employee!=null) {
            if(!employee.isStatus()){
                throw  new  DisabledAccountException();
            }
           // 参数1 :身份信息   参熟2:凭证信息(密码)  参数3:盐 参熟4:当前Realm的名称
            return new SimpleAuthenticationInfo(employee, employee.getPassword(),
                    ByteSource.Util.bytes(employee.getName()),this.getName());
        }
        return null;
    }


       /* SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //创建一个空的角色集合
        List<String> roles = new ArrayList<>();
        //添加角色1和角色2
            roles.add("role1");
            roles.add("role2");
        List<String> permissions = new ArrayList<>();
            permissions.add("user:list");
            permissions.add("user:delete");
            permissions.add("user:update");
        //将角色信息添加到授权信息对象中
            info.addRoles(roles);
            info.addStringPermissions(permissions);
            return info;*/

    //获取授权信息
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("=================");
       //创建一个空的授权信息对象
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获取到当前登录的用户
        Employee employee = (Employee) principals.getPrimaryPrincipal();
        //如果是超级管理员，授予其admin的角色和所有权限
        if(employee.isAdmin()){
            info.addRole("admin");
            info.addStringPermission("*:*");
        }else{
            //根据登录用户的id查询到它所有角色的编码
            List<String> roleSns = roleMapper.selectRoleSnsByEmpId(employee.getId());
            //根据登录用户的id查询所有权限表达式
            List<String> expressions = permissionMapper.selectByEmpId(employee.getId());
            //将用户拥有的角色和权限信息添加到授权信息对象中，供Shiro权限校验时使用
            info.addRoles(roleSns);
            info.addStringPermissions(expressions);
        }
        //返回授权信息
        return info;
    }

}